package com.zdkj.subway.support.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.ObjectUtils;
import com.zdkj.subway.support.dto.in.SwParamExgTypeDTO;
import com.zdkj.subway.support.entity.SwParamExgType;
import com.zdkj.subway.support.entity.SwParamReceipt;
import com.zdkj.subway.support.service.SwParamExgTypeService;
import com.zdkj.subway.support.util.UploadDataListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 交易类型表
 *
 * @author cui_com
 * @date 2020-01-08 17:13:41
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/swparamexgtype")
@Api(value = "swparamexgtype", tags = "交易类型表管理")
public class SwParamExgTypeController {

    private final SwParamExgTypeService swParamExgTypeService;

    /**
     * 分页查询
     *
     * @param page           分页对象
     * @param swParamExgType 交易类型表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwParamExgTypePage(Page page, SwParamExgType swParamExgType) {
        List<String> dateConditionhs = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(swParamExgType.getDates1())) {
            dateConditionhs = swParamExgType.getDates1();
        }
        if(ObjectUtil.isNotEmpty(dateConditionhs)){
            return R.ok(swParamExgTypeService.page(page, Wrappers.query(swParamExgType).lambda().between(SwParamExgType::getCreateTime,dateConditionhs.get(0), dateConditionhs.get(1))));
        }
        return R.ok(swParamExgTypeService.page(page, Wrappers.query(swParamExgType)));
    }


    /**
     * 通过id查询交易类型表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        return R.ok(swParamExgTypeService.getById(id));
    }

    /**
     * 新增交易类型表
     *
     * @param swParamExgType 交易类型表
     * @return R
     */
    @ApiOperation(value = "新增交易类型表", notes = "新增交易类型表")
    @SysLog("新增交易类型表")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('support_sw_param_exg_type_add')")
    public R save(@RequestBody SwParamExgTypeDTO swParamExgType) {
        SwParamExgType defaultObj = ObjectUtils.getDefaultObj(SwParamExgType.class);
        BeanUtils.copyProperties(swParamExgType, defaultObj);
        return R.ok(swParamExgTypeService.save(defaultObj));
    }

    /**
     * 修改交易类型表
     *
     * @param swParamExgType 交易类型表
     * @return R
     */
    @ApiOperation(value = "修改交易类型表", notes = "修改交易类型表")
    @SysLog("修改交易类型表")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('support_sw_param_exg_type_edit')")
    public R updateById(@RequestBody SwParamExgTypeDTO swParamExgType) {
        SwParamExgType obj = new SwParamExgType();
        BeanUtils.copyProperties(swParamExgType, obj);
        return R.ok(swParamExgTypeService.updateById(obj));
    }

    /**
     * 通过id删除交易类型表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除交易类型表", notes = "通过id删除交易类型表")
    @SysLog("通过id删除交易类型表")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('support_sw_param_exg_type_del')")
    public R removeById(@PathVariable Integer id) {
        return R.ok(swParamExgTypeService.removeById(id));
    }

    /**
     * 导入交易类型参数信息
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "导入交易类型参数信息", notes = "导入交易类型参数信息")
    @SysLog("导入交易类型参数信息")
    @PostMapping("/import")
    public R importExcel(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SwParamReceipt.class,
                    new UploadDataListener(swParamExgTypeService, SwParamExgType.class)).sheet().doRead();
            return R.ok("导入成功");
        } catch (IOException e) {
            log.info(e.getMessage());
            return R.ok("导入失败");
        }
    }

}
